#!/usr/bin/python3
# -*- coding: utf-8 -*-

# @file main.py
# @brief
# @author QRS
# @version 1.0
# @date 2022-02-17 21:43


import argparse
import multiprocessing

from frepai.utils.logger import frepai_get_logger
from frepai.core.handlers.service import TaskServiceHandler
from frepai.core.message import (
    MessageHandler,
    MainLooper,
)
from frepai.core.preprocess import PreProcessTask
from frepai.core.postprocess import PostProcessTask
from frepai.core.engine import EngineTask


multiprocessing.set_start_method('forkserver', force=True)
logger = frepai_get_logger('hzcsfrep', level='info', mp=True)

parser = argparse.ArgumentParser(prog='main')
parser.add_argument('--pre', action='store_true')
parser.add_argument('--post', action='store_true')
parser.add_argument('--engine', action='store_true')


def main(args):
    MessageHandler.logger = logger
    main_loop = MainLooper()
    main_loop.add_handler(TaskServiceHandler())
    H = main_loop.default_handler
    if args.pre:
        PreProcessTask(logger, H).start()

    if args.post:
        PostProcessTask(logger, H).start()

    if args.engine:
        EngineTask(logger, H).start()

    main_loop.start()
    main_loop.join()


if __name__ == "__main__":
    main(parser.parse_args())
